package org.lex.swing.widgets;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;

import javax.swing.JLabel;

import org.lex.swing.Swings;


public class GradientLabel extends JLabel {
	private Color c1 = null;
	private Color c2 = null;
	private Point p1 = new Point();
	private Point p2 = new Point();

	public static void main(String[] args) {
		// color model in windows-media-center start menu
		Color c1 = new Color(118, 161, 222);
		Color c2 = new Color(63, 102, 188);
		GradientLabel label = new GradientLabel(c1, c2);
		Swings.show(label, false);
	}

	public GradientLabel(Color c1, Color c2) {
		super();
		this.setColors(c1, c2);
	}

	public void setColors(Color color1, Color color2) {
		if (null == color1 || null == color2)
			throw new NullPointerException("color is null");
		this.c1 = color1;
		this.c2 = color2;
		this.repaint();
	}

	/**
	 * <pre>
	 * +-----------------+
	 * |  c1
	 * |  v
	 * |  c2
	 * +-----------------+
	 * </pre>
	 */
	@Override
	protected void paintComponent(Graphics g) {
		int width = this.getWidth();
		int height = this.getHeight();
		p1.setLocation(0, 0);
		p2.setLocation(0, height);
		Graphics2D g2 = (Graphics2D) g;
		GradientPaint paint = new GradientPaint(p1, c1, p2, c2);
		g2.setPaint(paint);
		g2.fillRect(0, 0, width, height);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		super.paintComponent(g2);
	}
}
